﻿using System;
using CSharpLogger.Common;

namespace CSharpLogger
{
    /// <summary>
    /// Helper class for getting or setting configurations needed for the Logger instance
    /// </summary>
    public class CSharpLoggerHelper
    {
        /// <summary>
        /// Get the Level asigned to the Logger object in the web.config file under appSettings section and the key Level 
        /// </summary>
        /// <remarks>
        /// This key/value pair MUST BE configured in the configuration file (web.conf or app.config) under the appSettings section
        /// 
        /// <configuration>
        ///     <appSettings>
        ///         <add key="Level" value="OFF"/>
        ///     </appSettings>
        /// </configuration>
        /// </remarks>
        /// <example>CSharpLogger.CSharpLoggerHelper.GetLevel()</example>
        /// <exception cref="NullReferenceException">If the key/value pair is not configured this exceptin will be thrown and the Level will be assigned to Level.ERROR </exception>
        /// <exception cref="Exception">If an unexpected error occurs the Level will be set to Level.OFF </exception>
        /// <returns>The Level asigned to the Logger instance</returns>
        public static LogLevel GetLevel()
        {
            try
            {
                string level = System.Configuration.ConfigurationManager.AppSettings["Level"].ToString();

                switch (level.ToUpper())
                {
                    case "OFF":
                        return LogLevel.OFF;
                    case "INFO":
                        return LogLevel.INFO;
                    case "WARN":
                        return LogLevel.WARN;
                    case "ERROR":
                        return LogLevel.ERROR;
                    case "FATAL":
                        return LogLevel.FATAL;
                    case "DEBUG":
                        return LogLevel.DEBUG;
                    default:
                        return LogLevel.OFF;
                }
            }
            catch (NullReferenceException)
            {
                return LogLevel.ERROR;
            }
            catch (Exception)
            {
                return LogLevel.OFF;
            }
        }
    }

   
    

}
